--极速押运
--#include "data\config\activityconfig\Escort\EscortConfig.lua" once



--双倍运镖开始
function EscortActivityStart()
	-- 广播
	System.broadcastTipmsg( Lang.ScriptTips.EscortTips038,ttScreenCenter+ttChatWindow )

	MobEscortMonsters()
end

--双倍运镖结束
function EscortActivityEnd()

	ClearEscortMonsters()
end

--发送数据
function SendEscortData(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local idx = sVar.myRefreshEscortIdx or 1
	local isDoubleTime = 0
	if EscortDoubleReward() then
		isDoubleTime = 1
	end
	local EscortTimes,EscortIncTimes = InitEscort(sysarg)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sEscortInitData)
    if netPack then
    	DataPack.writeByte(netPack, EscortTimes)
    	DataPack.writeByte(netPack, EscortIncTimes)
    	DataPack.writeByte(netPack, idx)
    	DataPack.writeByte(netPack, isDoubleTime)
	    DataPack.flush(netPack)
    end
end

--刷新镖车
function RefreshEscort(sysarg, packet)
	local oneKey = DataPack.readByte(packet)
	--print("oneKey ="..oneKey)
	local sVar = Actor.getStaticVar(sysarg)
	if not CheckEscortCond(sysarg, true) then
		return
	end
	local idx = sVar.myRefreshEscortIdx or 1
	--print("idx ="..idx)
	local config = EscortConfig.EscortList[idx]
	if not config then
		return
	end


	if not config.ConsumeCfg or idx == #EscortConfig.EscortList then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips036,ttFlyTip)
		return
	end

	if sVar.escortRobCount and sVar.escortRobCount > 0 then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips032,ttFlyTip)
		return
	end

	if oneKey and oneKey == 1 then
		if not CheckConsumeCond( sysarg, EscortConfig.OneKeyConsumes, 1 ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
			return
		end

		if not DoConsumeCond( sysarg, EscortConfig.OneKeyConsumes, 1, LogIds.EscortLogId, Lang.LogTips.Escort ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
			return
		end

		sVar.myRefreshEscortIdx = #EscortConfig.EscortList

		SendEscortData(sysarg)
	else

		if not CheckConsumeCond( sysarg, config.ConsumeCfg, 1 ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
			return
		end

		if not DoConsumeCond( sysarg, config.ConsumeCfg, 1, LogIds.EscortLogId, Lang.LogTips.Escort ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
			return
		end

		local rate = System.getRandomNumber(10000)
		if config.rate then
			local totalRate = 0
			local oldIdx = idx
			for ii = 1,  #config.rate do
				local curRate = config.rate[ii]
				if rate >= totalRate and rate < curRate + totalRate then
					sVar.myRefreshEscortIdx = idx + ii - 1
					break
				end
				totalRate = curRate + totalRate
			end
			if oldIdx ~= sVar.myRefreshEscortIdx then
				Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips034,ttFlyTip)
				SendEscortData(sysarg)
			else
				Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips035,ttFlyTip)
			end
		end
	end
end

--Init
function InitEscort(sysarg)
    local sVar = Actor.getStaticVar(sysarg)
	if not sVar.EscortTimes then
        sVar.EscortTimes = 0
	end
    if not sVar.EscortIncTimes then
	    sVar.EscortIncTimes = 0
	end
	return sVar.EscortTimes,sVar.EscortIncTimes
end

--禁止状态
function GetEscortForbidState()
	local nHour,nMin,nSec = System.getTime(0,0,0,0)    --当前时间
	for idx = 1, #EscortConfig.forbidTime do
		local time = EscortConfig.forbidTime[idx]
		local timeBegin = time[1]
		local timeEnd 	= time[2]
		if timeBegin[1] == timeEnd[1]  then    		--起止小时是同一个小时
			if timeBegin[2] <= nMin and nMin < timeEnd[2] then	--不在分钟范围之内
				return true
			end
		elseif timeBegin[1] < timeEnd[1] then		--开始小时小于结束小时
			if nHour > timeBegin[1] and timeEnd[1] > nHour then
				return true
			elseif nHour == timeBegin[1] and timeBegin[2] <= nMin then
				return true
			elseif nHour == timeEnd[1] and nMin < timeEnd[2] then
				return true
			end
		end
	end
	return false
end

--开始押运
function StartEscort(sysarg)
	if not CheckEscortCond(sysarg, false) then
		return
	end
	if IsEscortForbid() then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips037,ttFlyTip)
		return
	end 
	local sVar = Actor.getStaticVar(sysarg)
	local dVar = Actor.getDyanmicVar(sysarg)
	--如果当前场景不是接任务场景
	local scendID = Actor.getSceneId(sysarg)
	if scendID ~=  EscortConfig.EscortSrc.sceneid then
	    return
	end
	local npcX, npcY = Actor.getNpcPos(EscortConfig.EscortSrc.sceneid,EscortConfig.EscortSrc.npcid,0,0);
    local selfPosX, selfPosY = Actor.getEntityPosition(sysarg, 0, 0)
	if (math.abs(npcX-selfPosX) >  EscortConfig.EscortNpcDist  or math.abs(npcY -selfPosY) > EscortConfig.EscortNpcDist) then
	    Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips033, ttFlyTip)
	    return
	end
    local idx = sVar.myRefreshEscortIdx or 1
    local cfg = EscortConfig.EscortList[idx]
	if not cfg then
       return
	end
	--[[
	local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
    --检查消耗
	local ConsumeIdx = 0
	for i=1,#cfg.ConsumeCfg do
	    if  level >= cfg.ConsumeCfg[i].MinLevel and level <= cfg.ConsumeCfg[i].MaxLevel then
		    ConsumeIdx = i
		end
	end
	if ConsumeIdx == 0 then
	    return 
	end
	--奖励索引
	local AwardIdx = 0
	for i=1,#cfg.AwardCfg do
	    if  level >= cfg.AwardCfg[i].MinLevel and level <= cfg.AwardCfg[i].MaxLevel then
		    AwardIdx = i
		end
	end
	if AwardIdx == 0 then
	    return 
	end
	local awards = cfg.AwardCfg[AwardIdx].Award
	local Consume = {cfg.ConsumeCfg[ConsumeIdx].Consume}
	if Consumes.Check(sysarg, Consume) ~= true then
		return
	end 
	--消耗
	if Consumes.Remove(sysarg, Consume, 227, Lang.Log.Escort) ~= true then
		return
	end
	]]

	local day = System.getDaysSinceOpenServer()
	local awards = GetElemByCond(cfg.AwardCfg, day)
	
	if not awards then
	    return 
	end

    local  Times, IncTimes = InitEscort(sysarg)
	local  TodayMaxTimes = EscortConfig.Times + IncTimes
	--[[
    TodayMaxTimes = TodayMaxTimes + Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
    ]]
	local x, y = Actor.getEntityPosition(sysarg, 0, 0)
	local monster = Actor.createMonster(sysarg, cfg.id, x, y, cfg.livetime,true)
    if (monster ~= nil) then
    	sVar.myRefreshEscortIdx = idx
	    Actor.closeDialogs(sysarg, enLinkEscortWnd)
		Actor.followActor(monster, sysarg, EscortConfig.distance[1], EscortConfig.distance[2], true)
	    Actor.setEntityTelByOwnderAttr(monster, true)     --设置实体是否跟着归属者传送
	    Actor.setEntityLazyFollow(monster, true)          --设置实体是否惰性跟随
	    Actor.setEntityCanAttackMonster(monster, false)
		--Actor.setDenyBeAttack(monster,true)
	    Actor.setDenyAttackedByActor(monster, true)       --设置实体是否不可被玩家攻击
	    --Actor.refreshActorObserveList(sysarg)              --刷新观察者，防止客户端延迟	
		--Actor.setAttackStillMove(monster,true)
        sVar.EscortBeginTime = System.getCurrMiniTime()    --开始护镖时间
        sVar.EscortHandle = Actor.getHandle(monster)       --怪物句柄
		--sVar.EscortIdx = idx
		dVar.EscortOpenNpcDialog = 0
        EscortRighPanel(sysarg,EscortConfig.EscortTime)		
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips002,ttFlyTip)
		Actor.setDartFlag(sysarg,true)
		--处理护镖超时
		Actor.unregScriptCallback(sysarg, "OnEscortTimeOut")
		Actor.regScriptTimer(sysarg, 0, EscortConfig.EscortTime  * 1000, EscortConfig.EscortTime  * 1000, 1, "OnEscortTimeOut")
		--处理镖车超距离
		Actor.unregScriptCallback(sysarg, "CheckEscortDist")
		Actor.regScriptTimer(sysarg, 0, EscortConfig.CheckTimer  * 1000, EscortConfig.CheckTimer  * 1000, -1, "CheckEscortDist")
		--播报
		local broadcast = cfg.broadcast or false
		if broadcast then
	        local strName = Actor.getName(sysarg)
		    local MonsterName = Actor.getName(monster)
	        local strNotice = string.format(Lang.ScriptTips.EscortMsg002,strName,MonsterName)
	        System.broadcastTipmsg(strNotice,ttScreenCenter+ttChatWindow)
		end

		--自动寻路
	    local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sEscortStart)
	    if netPack then
	    	DataPack.writeByte(netPack, 1)
		    DataPack.flush(netPack)
	    end
	    
	    Actor.triggerQuestEvent(sysarg, 45, 3, 1)

	    Actor.onTakeOffRide(sysarg)		--下马
	    SendActivityLog(sysarg, ACTIVITY_DEFINE_EACORT, ActLogStatus_Join, true)
	end
end

--重置玩家变量
function ResetActorEscort(sysarg, needResetIdx)
	if not needResetIdx then
		needResetIdx = false
	end
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if EscortHandle > 0 then
		Fuben.clearMonster(EscortHandle, false)
	end
	sVar.EscortHandle = 0
	sVar.EscortBeginTime = 0
	if needResetIdx then
		sVar.myRefreshEscortIdx = 1
		sVar.escortRobCount  = 0
	end
	--sVar.EscortAwardIdx = 0
	CloseActivityRightPanel(sysarg)
	--Actor.closeActivityWind(sysarg)
	Actor.setDartFlag(sysarg,false)
	Actor.unregScriptCallback(sysarg, "OnEscortTimeOut")
	Actor.unregScriptCallback(sysarg, "CheckEscortDist")
end

--取消押运
function CancelEscort(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if  EscortHandle == 0 then
	    return
	end
    ResetActorEscort(sysarg)
	Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips012,ttFlyTip)
end


--押运超时
function  OnEscortTimeOut(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if  EscortHandle == 0 then
	    return
	end
    ResetActorEscort(sysarg)
    SendEscortRestartType(sysarg, 1)
	--Actor.messageBox(sysarg,0,0,Lang.ScriptTips.EscortTips013,Lang.ScriptTips.EscortTips030.."/EscortConfirm",nil,nil)
end

--检测镖车距离
function  CheckEscortDist(sysarg)
    InitEscort(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if  EndEscortHandle == 0 then
	    return
	end
	--是否有镖车
	local Monster = Actor.getEntity(EscortHandle)
	if(Monster == nil)then
		return
	end
	--镖车距离检测&攻击检测
	local monPosX, monPosY = Actor.getEntityPosition(Monster, 0, 0)
    local monSceneName = Actor.getSceneName(Monster,0,0)
    local monSceneId = Actor.getSceneId(Monster)
	local selfPosX, selfPosY = Actor.getEntityPosition(sysarg, 0, 0)
	local currentTime = System.getCurrMiniTime() 
	UpdateEscortRightPanel(sysarg)
	if monSceneId == EscortConfig.EscortDest.sceneid then
	    if (math.abs(monPosX - EscortConfig.EscortDest.x) <= EscortConfig.OpenNpcDist and  math.abs(monPosY-EscortConfig.EscortDest.y) <=  EscortConfig.OpenNpcDist) then
		    local dVar = Actor.getDyanmicVar(sysarg)
			local OpenNpcDialog = dVar.EscortOpenNpcDialog or 0
			if OpenNpcDialog == 2 then
			    return
		    end
            
            --自动寻路
		    local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sEscortStart)
		    if netPack then
		    	DataPack.writeByte(netPack, 1)
			    DataPack.flush(netPack)
		    end

			dVar.EscortOpenNpcDialog = OpenNpcDialog + 1
	    end
	end
end

--镖车死亡
function OnEscortPlayerDie(sysarg, killerPtr)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	--是否有镖车
	local Monster = Actor.getEntity(EscortHandle)
	if not Monster then
		return
	end
	local Owner = Actor.getMonsterOwner(Monster)
	if Owner and Owner == sysarg then
	    ResetActorEscort(sysarg)

	    SendEscortRestartType(sysarg, 2)
		--Actor.messageBox(sysarg,0,0,Lang.ScriptTips.EscortTips014,Lang.ScriptTips.EscortTips030.."/EscortConfirm",nil,nil)
	end

	if not EscortDoubleReward() then
		Actor.sendTipmsg( killerPtr, Lang.ScriptTips.EscortTips001,ttFlyTip)
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	local robCount = sVar.escortRobCount or 0
	if robCount >= EscortConfig.dieDropCount then
		Actor.sendTipmsg( killerPtr, Lang.ScriptTips.EscortTips007,ttFlyTip)
		return
	end

	local Idx = sVar.myRefreshEscortIdx or 0 
	if Idx == 0  then
	    return
	end

	local escortCfg = EscortConfig.EscortList[Idx]
	if  not escortCfg then
       return
	end

	local day = System.getDaysSinceOpenServer()
	local awards = GetElemByCond(EscortConfig.EscortList[Idx].AwardCfg, day)
	if not awards then
		return
	end

	local awardCfg = awards.Award
	if not awardCfg then
       return
	end

	if EscortDoubleReward() then
		awardCfg = GetAwardsByRate(awardCfg, EscortConfig.factor)
	end	

	awardCfg = GetAwardsByRate(awardCfg, EscortConfig.dieDropPrcent)

	robCount = robCount + 1

	sVar.escortRobCount = robCount
		
	local logId, logStr = LogIds.EscortLogId, Lang.LogTips.Escort
	if IsBagGridEnough(killerPtr, awardCfg) then
		GiveCommonAwardEx(killerPtr, awardCfg, logId, logStr)
	else
	   	local actorId = Actor.getActorId(killerPtr)
		local title = Lang.ScriptTips.EscortMailTitle
		local content = Lang.ScriptTips.EscortMailContent
		Awards.GiveToMailById(actorId, awardCfg, title, content, logId, logStr)
	end

	local killerName = Actor.getName(killerPtr)
	local sysargName = Actor.getName(sysarg)
    local strNotice = string.format(Lang.ScriptTips.EscortMsg003,sysargName,killerName)
    System.broadcastTipmsg(strNotice,ttScreenCenter+ttChatWindow)

	Actor.sendTipmsg( killerPtr, string.format(Lang.ScriptTips.EscortTips011, EscortConfig.dieDropPrcent * 100),ttFlyTip)
	--Actor.refreshActorObserveList(sysarg)
end

--提交押运
function  HandinEscort(sysarg)
    InitEscort(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	--是否有镖车
	local Monster = Actor.getEntity(EscortHandle)
	if(Monster == nil)then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips003,ttFlyTip)
		return
	end
	--超时
	local  EscortBeginTime = sVar.EscortBeginTime or 0
    if  (EscortBeginTime + EscortConfig.EscortTime) <  System.getCurrMiniTime() then
	    OnEscortTimeOut(sysarg)
		return
	end
	--镖车距离检测
	local monPosX, monPosY = Actor.getEntityPosition(Monster, 0, 0)
	local selfPosX, selfPosY = Actor.getEntityPosition(sysarg, 0, 0)
	local succFollowDist = EscortConfig.FinishsuccDist
	if (math.abs(monPosX-selfPosX) > succFollowDist or math.abs(monPosY-selfPosY) > succFollowDist) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips015,ttFlyTip)
		return true
	end
    local MonsterName = Actor.getName(Monster)
    --[[
	--血量检测
	local maxHp = Actor.getIntProperty(Monster, PROP_CREATURE_MAXHP)
	local hp    = Actor.getIntProperty(Monster, PROP_CREATURE_HP)
    local hpRate =  math.floor((hp / maxHp) * 100)
	local hpAward = false
    if hpRate >= EscortConfig.HpAwardRate then
	    hpAward = true
	end
	]]
	Fuben.clearMonster(EscortHandle, false)
	CloseActivityRightPanel(sysarg)
	Actor.unregScriptCallback(sysarg, "OnEscortTimeOut")
	Actor.unregScriptCallback(sysarg, "CheckEscortDist")
	Actor.setDartFlag(sysarg,false)
	--发奖
	local Idx = sVar.myRefreshEscortIdx or 0 
	if Idx == 0  then
		--print("Idx ="..Idx)
	    return
	end
    local escortCfg = EscortConfig.EscortList[Idx]
	if  not escortCfg then
       return
	end
	local day = System.getDaysSinceOpenServer()
	local awards = GetElemByCond(EscortConfig.EscortList[Idx].AwardCfg, day)
	if not awards then
		--print("111")
		return
	end
    local awardCfg = awards.Award
	if not awardCfg then
		--print("22")
       return
	end
	if EscortDoubleReward() then
		awardCfg = GetAwardsByRate(awardCfg, EscortConfig.factor)
	end		

	local robCount = sVar.escortRobCount or 0
	
	if robCount and robCount > 0 then
		awardCfg = GetAwardsByRate(awardCfg, (1 - robCount * EscortConfig.dieDropPrcent))
	end

	local logId, logStr = LogIds.EscortLogId, Lang.LogTips.Escort
	if IsBagGridEnough(sysarg, awardCfg) then
		GiveCommonAwardEx(sysarg, awardCfg, logId, logStr)
	else
	   	local actorId = Actor.getActorId(sysarg)
		local title = Lang.ScriptTips.EscortMailTitle
		local content = Lang.ScriptTips.EscortMailContent
		Awards.GiveToMailById(actorId, awardCfg, title, content, logId, logStr)
	end
	sVar.EscortTimes = sVar.EscortTimes + 1
	sVar.EscortBeginTime = 0
	sVar.EscortHandle = 0
	sVar.myRefreshEscortIdx = 1
	sVar.escortRobCount = 0
	--Actor.closeNPCDialog(sysarg)
    Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips004,ttFlyTip)
	local broadcast = escortCfg.broadcast or false
	if broadcast then	
	    local strName = Actor.getName(sysarg)
	    local strNotice = string.format(Lang.ScriptTips.EscortMsg001,strName,MonsterName)
	    System.broadcastTipmsg(strNotice,ttScreenCenter+ttChatWindow)
	end
	--Actor.setWorkDayMask(sysarg, 0, 12, true)
	--AddDailyActivityDoneNum(sysarg, enDailyActId_Escort, 1)		--进行急速押运1次
	SendActivityLog(sysarg, ACTIVITY_DEFINE_EACORT, ActLogStatus_Complete, true)
	--次数
    local  Times, IncTimes = InitEscort(sysarg)
	local  ActorMaxTimes = EscortConfig.Times  + IncTimes

	SendEscortData(sysarg)
	--[[
    ActorMaxTimes = ActorMaxTimes + Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
    ]]
	--还有剩余次数
	SendEscortRestartType(sysarg, 3)


	Actor.triggerAchieveEvent(sysarg, 44, 11)
	--[[
	if  ActorMaxTimes > Times then
	    local msg = string.format(Lang.ScriptTips.EscortTips025,ActorMaxTimes - Times)
        Actor.messageBox(sysarg,0,0,msg,Lang.ScriptTips.EscortTips020.."/GoonEscort",Lang.ScriptTips.EscortTips031.."/EscortCancel",nil)
	else
	    local msg = Lang.ScriptTips.EscortTips027
	    Actor.messageBox(sysarg,0,0,msg,Lang.ScriptTips.EscortTips029.."/ShowActivityWin",Lang.ScriptTips.EscortTips031.."/EscortCancel",nil)
	end
	]]
end

function ShowActivityWin(sysarg)
    Actor.openDialogs(sysarg, enLinkActivityGuideWnd)
end

--继续押运
function  GoonEscort(sysarg)
    Actor.enterScene(sysarg,EscortConfig.EscortSrc.sceneid,EscortConfig.EscortSrc.x, EscortConfig.EscortSrc.y)
   	Actor.openDialogs(sysarg, enLinkEscortWnd) 
end


--条件检测
function  CheckEscortCond(sysarg, isBuyCount)
    --等级
    if Actor.checkActorLevel(sysarg, EscortConfig.level[2], EscortConfig.level[1]) == false then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips008, ttFlyTip)
		return false
	end

	--次数
    local  Times, IncTimes = InitEscort(sysarg)
	local  TodayMaxTimes = EscortConfig.Times + EscortConfig.IncTimes 
	TodayMaxTimes = TodayMaxTimes + Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
	local ActorMaxTimes = EscortConfig.Times + IncTimes + Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
	if  Times >= ActorMaxTimes and  Times < TodayMaxTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips021, ttFlyTip)
		return false
    elseif Times >= TodayMaxTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips005, ttFlyTip)
		return false
	end
	--是否已有镖车
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if  EscortHandle > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
		return false
	end

	local IsEscortState = Actor.getIsDartFlag(sysarg)
	if IsEscortState then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
		return false
	end

	if not isBuyCount then
		--队伍
		if Actor.isInTeam(sysarg) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips009, ttFlyTip)
			return false
		end
		--是否有团队副本
		if Actor.hasState(sysarg,esStateTeamFuben) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips022, ttFlyTip)
			return false  
		end
	end

	return true
end

--玩家上线
function OnEscortLogin(sysarg)
    --等级
    if Actor.checkActorLevel(sysarg, EscortConfig.level[2], EscortConfig.level[1]) == false then
	    return
	end
	--判断是否还在押运状态
    local  sVar = Actor.getStaticVar(sysarg)
	local  dVar = Actor.getDyanmicVar(sysarg)
	local  EscortBeginTime = sVar.EscortBeginTime or 0
    local  idx = sVar.myRefreshEscortIdx or 0
    if  (EscortBeginTime + EscortConfig.EscortTime) > System.getCurrMiniTime() and idx > 0 then
        local cfg = EscortConfig.EscortList[idx]
	    if  not cfg then
           return
	    end
	    local day = System.getDaysSinceOpenServer()
	    local myAwards = GetElemByCond(cfg.AwardCfg, day)
	    if not myAwards then
	    	return
	    end
		local awards = myAwards.Award
        local  Times, IncTimes = InitEscort(sysarg)
	    local  TodayMaxTimes = EscortConfig.Times + IncTimes
	    --[[
		TodayMaxTimes = TodayMaxTimes + Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
		]]
	    local x, y = Actor.getEntityPosition(sysarg, 0, 0)
	    local monster = Actor.createMonster(sysarg, cfg.id, x, y, cfg.livetime,true)
        if (monster ~= nil) then
		    Actor.followActor(monster, sysarg, EscortConfig.distance[1], EscortConfig.distance[2], true)   --跟随某个玩家
	        Actor.setEntityTelByOwnderAttr(monster, true)     --设置实体是否跟着归属者传送
	        Actor.setEntityLazyFollow(monster, true)          --设置实体是否惰性跟随
	        --Actor.setDenyAttackedByActor(monster, true)       --设置实体是否不可被玩家攻击
	        --Actor.refreshActorObserveList(sysarg)              --刷新观察者，防止客户端延迟
	        Actor.setEntityCanAttackMonster(monster, false)
			--Actor.setDenyBeAttack(monster,true)
	    	Actor.setDenyAttackedByActor(monster, true)       --设置实体是否不可被玩家攻击
	    	--Actor.refreshActorObserveList(sysarg)              --刷新观察者，防止客户端延迟	
			--Actor.setAttackStillMove(monster,true)
            sVar.EscortHandle = Actor.getHandle(monster)       --怪物句柄
			dVar.EscortOpenNpcDialog = 0
            local leaveTime = EscortBeginTime + EscortConfig.EscortTime  - System.getCurrMiniTime()			
            EscortRighPanel(sysarg,leaveTime)		
		    Actor.sendTipmsg( sysarg, Lang.ScriptTips.EscortTips002,ttFlyTip)
		    Actor.setDartFlag(sysarg,true)
		    -- 处理护镖超时
		    Actor.unregScriptCallback(sysarg, "OnEscortTimeOut")
		    Actor.regScriptTimer(sysarg, 0, leaveTime  * 1000, leaveTime  * 1000, 1, "OnEscortTimeOut")
		    --处理镖车超距离
		    Actor.unregScriptCallback(sysarg, "CheckEscortDist")
		    Actor.regScriptTimer(sysarg, 0, EscortConfig.CheckTimer  * 1000, EscortConfig.CheckTimer  * 1000, -1, "CheckEscortDist")
	    end
	else
	    sVar.EscortHandle = 0
	    sVar.EscortBeginTime = 0
	    --sVar.myRefreshEscortIdx = 0
	    --sVar.escortRobCount  = 0
    end	
end

--玩家下线
function OnEscortLogout(sysarg)
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	--是否有镖车
	local Monster = Actor.getEntity(EscortHandle)
	if(Monster ~= nil)then
		Fuben.clearMonster(EscortHandle, false)
	end
	sVar.EscortHandle = 0
	Actor.setDartFlag(sysarg,false)
	Actor.unregScriptCallback(sysarg, "OnEscortTimeOut")
	Actor.unregScriptCallback(sysarg, "CheckEscortDist")
end

--每日重置押运次数
function ResetEscortTimes(sysarg)
    local sVar = Actor.getStaticVar(sysarg)
    sVar.EscortTimes = 0
	sVar.EscortIncTimes = 0
	sVar.escortRobCount = 0
end


--增加次数
function  AddEscortCount(sysarg)

	if Actor.checkActorLevel(sysarg, EscortConfig.level[2], EscortConfig.level[1]) == false then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips008, ttFlyTip)
		return false
	end

    local  Times, IncTimes = InitEscort(sysarg)
	local  vipTimes = Actor.getConcurrentVipPriv(sysarg, enConcVipPriv_Escort)
	local  TodayMaxTimes = EscortConfig.Times + EscortConfig.IncTimes + vipTimes
	local  ActorMaxTimes = EscortConfig.Times + IncTimes + vipTimes
	if ActorMaxTimes >= TodayMaxTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips005, ttFlyTip)
		return false
	end
	if IncTimes >= EscortConfig.IncTimes then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips018, ttFlyTip)
		return false
	end

	--是否已有镖车
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	if  EscortHandle > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
		return false
	end

	local IsEscortState = Actor.getIsDartFlag(sysarg)
	if IsEscortState then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
		return false
	end
	--[[
	local msg = string.format(Lang.ScriptTips.EscortTips016,EscortConfig.IncTimesItem.count,Item.getItemName(EscortConfig.IncTimesItem.id))
    Actor.messageBox(sysarg,0,0,msg,Lang.ScriptTips.confirm.."/IncEscortTimesConfirm",Lang.ScriptTips.cancel.."/EscortCancel",nil)
    ]]

    local Consume = {EscortConfig.IncTimesItem}
    if not CheckConsumeCond( sysarg, Consume, 1 ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, Consume, 1, LogIds.EscortLogId, Lang.LogTips.Escort ) then
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.MaterialFuBen002, ttFlyTip)
		return
	end
	
    local sVar = Actor.getStaticVar(sysarg)
    sVar.EscortIncTimes =  sVar.EscortIncTimes + 1
	Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips017, ttFlyTip)

	SendEscortData(sysarg)
end

function  EscortConfirm(sysarg)
	Actor.enterScene(sysarg,EscortConfig.EscortSrc.sceneid,EscortConfig.EscortSrc.x, EscortConfig.EscortSrc.y)
   	Actor.openDialogs(sysarg, enLinkEscortWnd) 
end

function EscortCancel(sysVar)

end

--是否在双倍活动中
function EscortDoubleReward()
	local dVar = System.getDyanmicVar()
	if dVar.escortDoubleState then
		return true
	end
	return false
end

function EscortRighPanel(sysarg,ActivityTime)
	local panelType = 2
	local activityId = enRightPanelActivity_Escort
	local panelInfo = {}
	panelInfo.sts = 1
	panelInfo.title = Lang.ScriptTips.Escort001
	panelInfo.timeTitle = Lang.ScriptTips.Escort002
	panelInfo.restTime = ActivityTime
	panelInfo.contents = {}

	table.insert(panelInfo.contents, Lang.ScriptTips.Escort003)

	if EscortDoubleReward() then
		local sVar = Actor.getStaticVar(sysarg)
		local robCount = sVar.escortRobCount or 0
		table.insert(panelInfo.contents, string.format(Lang.ScriptTips.Escort004, robCount, EscortConfig.dieDropCount))
		table.insert(panelInfo.contents, Lang.ScriptTips.Escort006)
	end

	panelInfo.buttons = {5, 7}

	OpenActivityRightPanel( sysarg, panelType, activityId, panelInfo )
end

function UpdateEscortRightPanel(sysarg)
	local panelInfo = {}
	panelInfo.contents = {}
	local succFollowDist = EscortConfig.FinishsuccDist
	local sVar = Actor.getStaticVar(sysarg)
	local EscortHandle = sVar.EscortHandle or 0
	--是否有镖车
	local Monster = Actor.getEntity(EscortHandle)
	if Monster then
		local monSceneId = Actor.getSceneId(Monster)
		local monPosX, monPosY = Actor.getEntityPosition(Monster, 0, 0)	
		local selfPosX, selfPosY = Actor.getEntityPosition(sysarg, 0, 0)	
		if (math.abs(monPosX-selfPosX) > succFollowDist or math.abs(monPosY-selfPosY) > succFollowDist) then
	        table.insert(panelInfo.contents, string.format(Lang.ScriptTips.Escort005, monSceneId, monPosX, monPosY))
	    else
	 		table.insert(panelInfo.contents, Lang.ScriptTips.Escort003)
	    end
		
		if EscortDoubleReward() then
			local sVar = Actor.getStaticVar(sysarg)
			local robCount = sVar.escortRobCount or 0
			table.insert(panelInfo.contents, string.format(Lang.ScriptTips.Escort004, robCount, EscortConfig.dieDropCount))
			table.insert(panelInfo.contents, Lang.ScriptTips.Escort006)
		end

		panelInfo.buttons = {5, 7}

		ChangeActivityRightPanel( sysarg, panelInfo )
	end
end

--双倍活动创建指引怪，增加活动标志，修改押镖中玩家奖励显示
function  MobEscortMonsters()
	local dVar = System.getDyanmicVar()
	dVar.escortDoubleState = 1
    for k,v in ipairs(EscortConfig.Monsters) do
	   	local  hScene = Fuben.getSceneHandleById(v.sceneid, 0)
	    if hScene then
		    Fuben.createMonster(hScene,v.monid,v.x,v.y,v.livetime,0)
	    end
	end

	local playerList = LuaHelp.getAllActorList()  --获取在线玩家对象列表
	if(playerList == nil)then
		return
	end
	for i=1, #playerList do
		UpdateEscortRightPanel(playerList[i])
	end
end

--双倍活动删除指引怪，删除活动标志，修改押镖中玩家奖励显示
function ClearEscortMonsters()
	local dVar = System.getDyanmicVar()
	dVar.escortDoubleState = nil
    for k,v in ipairs(EscortConfig.Monsters) do
	   	local  hScene = Fuben.getSceneHandleById(v.sceneid, 0)
	    if hScene then
		    Fuben.clearMonster(hScene,v.monid)
	    end
	end

	local playerList = LuaHelp.getAllActorList()  --获取在线玩家对象列表
	if(playerList == nil)then
		return
	end
	for i=1, #playerList do
		UpdateEscortRightPanel(playerList[i])
	end
end

function SendEscortRestartType(sysarg, type)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sEscortNotice)
    if netPack then
    	--print("type = "..type)
    	DataPack.writeByte(netPack, type)
	    DataPack.flush(netPack)
    end
end

function IsEscortForbid()
	local dVar = System.getDyanmicVar()
	if dVar.escortForbidStart and dVar.escortForbidStart == 1 then
		return true
	end
	return false
end

function EscortForbidStart()
	local dVar = System.getDyanmicVar()
	dVar.escortForbidStart = 1
end

function EscortForbidEnd()
	local dVar = System.getDyanmicVar()
	dVar.escortForbidStart = nil
end





 
